﻿
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CSM.Models;
using CSM.Controllers;
using System.Collections.Generic;

namespace CSM.Controllers
{
    public class CSMLogin
    {
        private CustomDataContext db = new CustomDataContext();
        private string lastError;
        private TCsm_JS_User currectUser;

        public bool isAccountLock
        {
            get { return currectUser.IsStop; }
        }

        public string LastError
        {
            get { return lastError; }
            private set { lastError = value; }
        }


        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="username">操作员ID</param>
        public CSMLogin(string username)
        {
            if (db.TCsm_JS_User.Count(u => u.UserID == username) == 0)
            {
                throw new Exception("没有该操作员ID");
            }
            currectUser = (from u in db.TCsm_JS_User
                           where u.UserID == username
                           select u).Single();
        }

        /// <summary>
        /// 检查密码
        /// </summary>
        /// <param name="password">密码</param>
        /// <param name="isHash">是否已经加密</param>
        /// <returns></returns>
        public int CheckPassword(string password, bool isHash)
        {
            if (!isHash)
                password = CSMAccount.EncryptPassword(password);
            if (password == currectUser.Password)
                return 0;
            else
            {
                lastError = "密码错误!";
                return -1;
            }
        }

        /// <summary>
        /// 检查密码
        /// </summary>
        /// <param name="ppassword">密码</param>
        /// <returns></returns>
        internal int CheckPassword(string password)
        {
            return CheckPassword(password, false);
        }

        /// <summary>
        /// 获得所有权限ID
        /// </summary>
        /// <returns></returns>
        public List<int> GetOwnAuthIDs()
        {
            CSMSecurity security = new CSMSecurity();
            var auths = currectUser.UserRightID.Split(',');
            var roles = currectUser.RoleID.Split(',');
            List<int> retval = new List<int>();
            foreach (var s in auths)
            {
                if (!string.IsNullOrEmpty(s))
                    retval.Add(Convert.ToInt32(s));
            }
            foreach (var s in roles)
            {
                var ret = security.GetAuthsOfRole(Convert.ToInt32(s));
                if (ret != null)
                    retval.AddRange(ret);
            }
            return retval;
        }



        /// <summary>
        /// 登录成功，赋值浏览器Session
        /// </summary>
        /// <returns></returns>
        public int Enter()
        {
            HttpContext.Current.Session[SystemVars.sysSessionUserId] = currectUser.UserID;
            HttpContext.Current.Session[SystemVars.sysSessionUserAuths] = GetOwnAuthIDs();
            return 0;
        }
    }
}
